﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UpdateApp
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string BaiduAppID = System.Configuration.ConfigurationManager.AppSettings["BaiduAppID"];
        string BaiduSecretKey = System.Configuration.ConfigurationManager.AppSettings["BaiduSecretKey"];

        protected void Page_Load(object sender, EventArgs e)
        {
            string word = Request["word"];

            if (string.IsNullOrEmpty(word))
            {
                Response.Write("");

                Response.End();
            }

            List<string> arr = word.Split('=').ToList();
            
            string temp = arr[0];

            TransApi transApi = new TransApi(BaiduAppID, BaiduSecretKey);
            string result = transApi.GetTransResult(temp, "auto", "auto").Trim();

            //当为中文时去除包含的英文
            if (Regex.IsMatch(temp.Substring(0, 1), "^[\u4e00-\u9fa5]{0,}$"))
            {
                temp = Regex.Replace(temp, "[a-z]", "", RegexOptions.IgnoreCase);

                arr[0] = result;
                arr.Add(temp);
            }
            //当为英文时，将英文置前，中文置后
            else
            {
                arr[0] = temp;
                arr.Add((result));
            }

            string row = string.Join("=", arr);

            Response.Write(row);
            Response.End();
        }
    }
}